#include "../mqserver/mq_virtualhost.hpp"
#include <gtest/gtest.h>

mq::VirtualHost::Ptr host;
class Mytest : public testing::Environment
{
public:
    void SetUp() override
    {
        // 不好在这里初始化对象，因为没有初始化列表。
        // 所以我们还是使用万能的指针。
        host = std::make_shared<mq::VirtualHost>("host-1","./data/host1/","./data/host1/test-host.db");

        std::unordered_map<std::string,std::string> empty_map;

        host->declareExchange("exchange1",mq::ExchangeType::DIRECT,true,false,empty_map);
        host->declareExchange("exchange2",mq::ExchangeType::DIRECT,true,false,empty_map);
        host->declareExchange("exchange3",mq::ExchangeType::DIRECT,true,false,empty_map);

        host->declareMsgqueue("queue1",true,false,false,empty_map);
        host->declareMsgqueue("queue2",true,false,false,empty_map);
        host->declareMsgqueue("queue3",true,false,false,empty_map);

        host->bind("exchange1","queue1","news.music.#");
        host->bind("exchange1","queue2","news.music.#");
        host->bind("exchange1","queue3","news.music.#");

        host->bind("exchange2","queue1","news.music.#");
        host->bind("exchange2","queue2","news.music.#");
        host->bind("exchange2","queue3","news.music.#");

        host->bind("exchange3","queue1","news.music.#");
        host->bind("exchange3","queue2","news.music.#");
        host->bind("exchange3","queue3","news.music.#");

        host->basicPublish("queue1",nullptr,"Hello World-1");
        host->basicPublish("queue1",nullptr,"Hello World-2");
        host->basicPublish("queue1",nullptr,"Hello World-3");

        host->basicPublish("queue2",nullptr,"Hello World-1");
        host->basicPublish("queue2",nullptr,"Hello World-2");
        host->basicPublish("queue2",nullptr,"Hello World-3");

        host->basicPublish("queue3",nullptr,"Hello World-1");
        host->basicPublish("queue3",nullptr,"Hello World-2");
        host->basicPublish("queue3",nullptr,"Hello World-3");

        host->allQueues();
    }
    void TearDown() override
    {
        host->clear();
    }
private:
    ;
public:
    ;
};

// TEST(Mytest,init_test)
// {
//     ASSERT_EQ(host->existExchange("exchange1"),true);
//     ASSERT_EQ(host->existExchange("exchange2"),true);
//     ASSERT_EQ(host->existExchange("exchange3"),true);

//     ASSERT_EQ(host->existMsgQueue("queue1"),true);
//     ASSERT_EQ(host->existMsgQueue("queue2"),true);
//     ASSERT_EQ(host->existMsgQueue("queue3"),true);

//     ASSERT_EQ(host->existBindings("exchange1","queue1"),true);
//     ASSERT_EQ(host->existBindings("exchange1","queue2"),true);
//     ASSERT_EQ(host->existBindings("exchange1","queue3"),true);

//     ASSERT_EQ(host->existBindings("exchange2","queue1"),true);
//     ASSERT_EQ(host->existBindings("exchange2","queue2"),true);
//     ASSERT_EQ(host->existBindings("exchange2","queue3"),true);

//     ASSERT_EQ(host->existBindings("exchange3","queue1"),true);
//     ASSERT_EQ(host->existBindings("exchange3","queue2"),true);
//     ASSERT_EQ(host->existBindings("exchange3","queue3"),true);

//     host->basicPublish("queue1",nullptr,"Hello World-1");
//     host->basicPublish("queue1",nullptr,"Hello World-2");
//     host->basicPublish("queue1",nullptr,"Hello World-3");

//     host->basicPublish("queue2",nullptr,"Hello World-1");
//     host->basicPublish("queue2",nullptr,"Hello World-2");
//     host->basicPublish("queue2",nullptr,"Hello World-3");

//     host->basicPublish("queue3",nullptr,"Hello World-1");
//     host->basicPublish("queue3",nullptr,"Hello World-2");
//     host->basicPublish("queue3",nullptr,"Hello World-3");

//     mq::MessagePtr msgp = host->basicConsume("queue1");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));

//     msgp = host->basicConsume("queue1");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-2"));

//     msgp = host->basicConsume("queue1");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-3"));

//     msgp = host->basicConsume("queue2");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));

//     msgp = host->basicConsume("queue2");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-2"));

//     msgp = host->basicConsume("queue2");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-3"));

//     msgp = host->basicConsume("queue3");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));

//     msgp = host->basicConsume("queue3");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-2"));

//     msgp = host->basicConsume("queue3");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-3"));
// }

// TEST(Mytest,remove_test)
// {
//     host->basicPublish("queue1",nullptr,"Hello World-1");
//     host->basicPublish("queue1",nullptr,"Hello World-1");
//     mq::MessagePtr msgp = host->basicConsume("queue1");
//     ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));
//     host->deleteQueue("queue1");
//     msgp = host->basicConsume("queue1");
//     ASSERT_EQ(msgp.get(),nullptr);
// }

TEST(HostTest,Ack_test)
{
    mq::MessagePtr msgp = host->basicConsume("queue1");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));
    host->basicAck("queue1",msgp->payload().properties().id());

    msgp = host->basicConsume("queue1");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-2"));
    host->basicAck("queue1",msgp->payload().properties().id());

    msgp = host->basicConsume("queue1");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-3"));
    host->basicAck("queue1",msgp->payload().properties().id());

    msgp = host->basicConsume("queue2");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));
    host->basicAck("queue2",msgp->payload().properties().id());

    msgp = host->basicConsume("queue2");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-2"));
    host->basicAck("queue2",msgp->payload().properties().id());

    msgp = host->basicConsume("queue2");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-3"));
    host->basicAck("queue2",msgp->payload().properties().id());

    msgp = host->basicConsume("queue3");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-1"));
    host->basicAck("queue3",msgp->payload().properties().id());

    msgp = host->basicConsume("queue3");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-2"));
    host->basicAck("queue3",msgp->payload().properties().id());

    msgp = host->basicConsume("queue3");
    ASSERT_EQ(msgp->payload().body(),std::string("Hello World-3"));
    host->basicAck("queue3",msgp->payload().properties().id());
}

int main()
{
    testing::InitGoogleTest();
    testing::AddGlobalTestEnvironment(new Mytest);
    RUN_ALL_TESTS();
}